/*
 *  File.cpp
 *  pcm
 *
 *  Created by Yanick Candaux on 15.12.11.
 *  Copyright 2011 Heig-vd. All rights reserved.
 *
 */


#include "File.h"
#include <cstring>


namespace pcm {

bool File::initializeQueue(){
		
	Node *node = new Node();
	node->setNext(NULL);
	Head = node;
	Tail = node;
	return true;
}

bool File::enq(BoardInfos *value){
	Node *last, *next;
	Node *node = getFreeNode();
	if (node == NULL) {
		return false;
	}
	node->setValue(value);
	node->setNext(NULL);
	while (true) {
		last = Tail;
		next = last->next();
		if (last == Tail) {
			if (next == NULL) {
				if (bcasptr(last->pointerNext(),next,node)) {
					break;
				}
				
			}else {
				bcasptr(&Tail,last,next);
			
			}

		}
		
	}
	bcasptr(&Tail,last,node);
	return true;
		
}
	

	

BoardInfos* File::deq(){
	Node *first, *last, *next;
	BoardInfos *value;
	while (true) {
		first = Head;
		last = Tail;
		next = first->next();
		if (first == Head) {
			if (first == last) {
				if (next == NULL) {
					return NULL;
				}
				bcasptr(&Tail, last,next);
			}else {
				value = next->value();
				if (bcasptr(&Head,first,next)) {
					freeNode(first);
					return value;
				}
			}

		}
	}
}

	
void File::freeNode(Node *node){

	delete node;
	

}

Node *File::getFreeNode(){
	Node *node = new Node();
		
	return node;	
}
	


	
}/* namespace pcm */
